#include<bits/stdc++.h>
using namespace std;
#define int long long

int dp[10005], w[105], v[105];

signed main() {
	int n, Q, W;
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> w[i] >> v[i];
	cin >> Q;
	while (Q--) {
		memset(dp, 0, sizeof dp);
		cin >> W;
		for (int i = 1; i <= n; i++) { // 遍历物品
			for (int j = W; j >= w[i]; j--) { // 遍历背包容量
				dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
			}
		}
		cout << dp[W] << endl;
	}
	return 0;
}
